<?php
namespace App\Services\Impl;
use App\Library\RepotsLibraryInterFace;
use App\Services\FileServiceInterFace;
use App\Services\RepotsServiceInterFace;
use App\Pojo\ResponsePojo;
use App\Utils\Utils;
use Illuminate\Support\Facades\Log;
use App\Constant\HttpStatus;

class RepotsServiceImpl implements RepotsServiceInterFace{

    private $repotsLibrary;
    private $fileServiceInterFace;
    public function __construct(RepotsLibraryInterFace $repotsLibraryInterFace, FileServiceInterFace $fileServiceInterFace)
    {
        $this->repotsLibrary = $repotsLibraryInterFace;
        $this->fileServiceInterFace = $fileServiceInterFace;
    }

    public function insertReport(array $credentials){
        $result = $this->repotsLibrary->insertReport($credentials);
        return $result;
    }


    public function getImageListByuserId($logId){
        Log::info('获取文件列表:');
        $logRepotJson = $this->repotsLibrary->getRepotsBylogId($logId);
        $logRepot = json_decode($logRepotJson, True);
        $fileHashs = [];
        foreach($logRepot as $item){
            $fileHashs[] = $item['fileId'];
        }

        $fileList = $this->fileServiceInterFace->getImageListByHashs($fileHashs);
        $files = [];
        $images = [];
        foreach ($fileList as $file) {
            if (is_object($file)) {
                $file = json_encode($file);
            }
            $file = json_decode($file, true);

            $file['file_path'] = asset('storage/' . $file['file_path']);

            if($file['file_type'] == 'image'){
                $images[] = $file;
            }else{
                $files[] = $file;
            }
        }
        
        return [
            'files' => $files,
            'images' => $images
        ];
       
    }

    public function getZhjyRepots($credentials){
        $paginator = $this->repotsLibrary->getZhjyRepots($credentials);

        $uploadedLogs = [];
        foreach ($paginator->items() as $log) {    
            $allfile = $this->getImageListByuserId($log['id']);
            $uploadedLogs[] = [
                'id' => $log['id'],
                'user_id' => $log['user_id'],
                'recordType' => $log['recordType'],
                'todaySummary' => $log['todaySummary'],
                'nextSummary' => $log['nextSummary'],
                'feedback' => $log['feedback'],
                'selfEvaluate' => Utils::CvanSelfEvaluateDbToHtml($log['selfEvaluate']),
                'images' => $allfile['images'],
                'files' => $allfile['files'],
                'filePath' => asset('storage/' . $log['filePath']),
                'fileName' => $log['fileName'],
                'createdAt' => $log['created_at'],
                'updated_at' => $log['updated_at'],
            ];
        }
        $LogList = [
            'list' => $uploadedLogs,
            'total' => $paginator->total(),
            'per_page' => $paginator->perPage(),
            'current_page' => $paginator->currentPage(),
            'last_page' => $paginator->lastPage()
        ];

        return ResponsePojo::success(HttpStatus::OK, HttpStatus::getStatusMessage(HttpStatus::OK), $LogList);
    }

    public function updateZhjyRepots($request){
        $credentials = $request->validate([
            'id' => 'required|integer',
            'todaySummary' => 'string',
            'nextSummary' => 'string',
            'feedback' => 'string',
            'selfEvaluate' => 'integer',
            'recordType' => 'integer',
            'oldFiles' => 'array',
            'newFiles' => 'array',
            'oldImages' => 'array',
            'newImages' => 'array',
        ]);
        $updatebody = [
            'id' => $credentials['id'],
            'todaySummary' => $credentials['todaySummary'],
            'nextSummary' => $credentials['nextSummary'],
            'feedback' => $credentials['feedback'],
            'selfEvaluate' => $credentials['selfEvaluate'],
            'recordType' => $credentials['recordType'],
            'oldFiles' => $credentials['oldFiles'],
            'newFiles' => $credentials['newFiles'],
            'oldImages' => $credentials['oldImages'],
            'newImages' => $credentials['newImages'],
        ];

        $this->repotsLibrary->updateZhjyRepots($updatebody);

        return ResponsePojo::success(HttpStatus::OK, HttpStatus::getStatusMessage(HttpStatus::OK), []);
    }

    public function deleteZhjyRepots($credentials){
        $this->repotsLibrary->deleteZhjyRepots($credentials);


        return ResponsePojo::success(HttpStatus::OK, HttpStatus::getStatusMessage(HttpStatus::OK), []);
    }
}